#pragma once
#include "VC_Drawable.h"
#include "C_SQLiteSpaceObject.h"
#include "C_SQLiteSpaceRegion.h"

class C_SpaceObjectFactory;
class VC_Object;

class C_Map : public VC_Drawable
{
private:
	float		m_cellSize;
	S_MapPoint	m_position;
	C_SQLiteSpaceObject* m_sql_spaceObj;
	C_SQLiteSpaceRegion* m_sql_spaceReg;
	C_SpaceObjectFactory* m_factoryObj;

	int m_wScrAmt;
	int m_hScrAmt;
	float m_wScr;
	float m_hScr;

	int m_xTexAmt;
	int m_yTexAmt;
	int m_wTexAmt;
	int m_hTexAmt;

	float m_xTex;
	float m_yTex;
	float m_wTex;
	float m_hTex;

	bool m_debugDraw;

	std::unique_ptr<S_MousePressMove> m_evMousePress;
	std::atomic<bool> m_ready;
	std::thread* m_proc;
	std::thread* m_procGen;
public:
	C_Map();
	virtual ~C_Map();

	////////////////////////////////////////////////////////////////////////////////
	//////	GET FUNCTION
	float* cellSize() { return &m_cellSize; }

	void doCount();
	void checkMap();


	void move(float x, float y);
	void generateMap(int x, int y);
	void zoom(float z);

	//////////////////////////////////////////////////////////////////////////
	////////	SETTING FUNCTIONS
	inline void setPos(int xi, int yi, float xf, float yf)
	{
		m_position = S_MapPoint::make(xi, yi, xf, yf);
		this->doCount();
		this->checkMap();
	}

	sf::Vector2f getPixCoord(int x, int y);

	/////////////////////////////////////////////////////////////
	/////////////	MAIN FUNCTIONS
	virtual void handleEvent(sf::Event e);
	virtual void render();
	virtual void update();



	///////////////////////////////////////////////////////////////
	/////////////	FACTORIES
	void addObject(VC_Object* obj);

	///////////////////////////////////////////////////////////////////////
	/////////// GETTING and SETTING
	int xTexAmt() { return m_xTexAmt; }
	int yTexAmt() { return m_yTexAmt; }
	int wTexAmt() { return m_wTexAmt; }
	int hTexAmt() { return m_hTexAmt; }

	void setXTexAmt(int v) { m_xTexAmt = v; }
	void setTTexAmt(int v) { m_yTexAmt = v; }
	void setWTexAmt(int v) { m_wTexAmt = v; }
	void setHTexAmt(int v) { m_hTexAmt = v; }

	C_SQLiteSpaceObject* spaceObj() { return m_sql_spaceObj; }
	C_SQLiteSpaceRegion* spaceReg() { return m_sql_spaceReg; }
	C_SpaceObjectFactory* factoryObj() { return m_factoryObj; }

	std::atomic<bool>& getReady() { return m_ready; }
};

void MainCheckMap(C_Map *map);

